<?php
class TF_UrlPersistence 
{
	protected $_session;
	protected function _getSession()
	{
		if (null === $this->_session) {
			$this->_session = new Zend_Session_Namespace('UrlPersistence');	
		}
		return $this->_session;
	}
	
	public function gotoLastUrl($default = ''){
		
		if($this->hasLastUrl()){
			$this->_gotoUrl($this->getLastUrl());
		} else {
			$this->_gotoUrl($default);
			
		}
	}
		
	protected function _gotoUrl($url)
	{
		$this->resetLastUrl();
		return Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector')
			->gotoUrlAndExit($url);
	}
	
	public function hasLastUrl()
	{
		return (bool)(null !== $this->_getSession->lastUrl);
	}
	
	public function resetLastUrl()
	{
		$this->_getSession->lastUrl = null;
	}
	
	public function getLastUrl()
	{
		return $this->_getSession->lastUrl;	
	}
	
	public function storeLastUrl($manualUrl = false)
	{
		$last = ($manualUrl ? $manualUrl : $_SERVER['REQUEST_URI']);
		$this->_getSession->lastUrl = $last;
	}
	
}